import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';

class AdjustProductNumberForm extends StatefulWidget {
  final String title;
  final String content;
  final String numberTitle;
  final ValueChanged<int> onNumberCallback;
  final int initialNumber;
  final int otherValue;
  const AdjustProductNumberForm(
      {Key key,
      this.title,
      this.content,
      this.otherValue,
      this.numberTitle,
      this.onNumberCallback,
      this.initialNumber})
      : super(key: key);

  @override
  _AdjustProductNumberFormState createState() =>
      _AdjustProductNumberFormState();
}

class _AdjustProductNumberFormState extends State<AdjustProductNumberForm> {
  TextEditingController controller = TextEditingController(text: '1');

  void _onReduce() {
    int integer = int.parse(controller.text, onError: (e) => 0);
    integer--;
    setTextControllerValue(integer);
  }

  void _onAdd() {
    int integer = int.parse(controller.text, onError: (e) => 0);
    integer++;
    setTextControllerValue(integer);
  }

  void setTextControllerValue(int value) {
    String string = value.toString();
    controller.value = TextEditingValue(
      text: string,
      selection: TextSelection.collapsed(offset: string.length),
    );
  }

  @override
  void initState() {
    super.initState();
    if (widget.initialNumber != null) {
      controller = TextEditingController(text: widget.initialNumber.toString());
    }
    controller.addListener(() {
      int integer = int.parse(controller.text, onError: (e) => null);
      if (integer == null || integer < 0) {
        setTextControllerValue(0);
        return;
      }
      widget.onNumberCallback?.call(integer);
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  void didUpdateWidget(covariant AdjustProductNumberForm oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.otherValue != widget.otherValue) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              widget.title,
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
                height: 16 / 14,
              ),
            ),
            SizedBox(
              height: getDp(6),
            ),
            Text.rich(TextSpan(children: [
              TextSpan(
                text: widget.content,
                style: TextStyle(
                  color: aceBlackColor,
                  fontSize: getSp(14),
                  fontWeight: FontWeight.w400,
                  height: 1,
                ),
              ),
              if (widget.otherValue != null && widget.otherValue != 0)
                TextSpan(
                  text:
                      '${widget.otherValue > 0 ? ' + ' : ' - '}${CashUtils.ignoreCashAmount(widget.otherValue)}',
                  style: TextStyle(
                    color: aceBlueColor,
                    fontSize: getSp(14),
                    fontWeight: FontWeight.w400,
                    height: 1,
                  ),
                )
            ])),
          ],
        ),
        Column(
          children: [
            Text(
              widget.numberTitle,
              style: TextStyle(
                color: aceBlackColor,
                fontSize: getSp(14),
                fontWeight: FontWeight.w400,
                height: 1,
              ),
            ),
            _buildNumberInput(),
          ],
        )
      ],
    );
  }

  _buildNumberInput() => Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          PressedOpacityButton(
            onPressed: _onReduce,
            child: SvgPicture.asset(
              A.assetsImagesStyleMinus,
              width: getDp(24),
              height: getDp(24),
            ),
          ),
          SizedBox(
            width: getDp(65),
            child: TextField(
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
              scrollPadding: EdgeInsets.zero,
              decoration: InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
              ),
              controller: controller,
              style: TextStyle(
                color: aceBlueColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          PressedOpacityButton(
            onPressed: _onAdd,
            child: SvgPicture.asset(
              A.assetsImagesStylePlus,
              width: getDp(24),
              height: getDp(24),
            ),
          ),
        ],
      );
}
